<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Calculate GCD and multipliers</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.gmp-gcd.html">gmp_gcd</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.gmp-hamdist.html">gmp_hamdist</a></div>
 <div class="up"><a href="ref.gmp.html">GMP 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.gmp-gcdext" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">gmp_gcdext</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.4, PHP 5)</p><p class="refpurpose"><span class="refname">gmp_gcdext</span> &mdash; <span class="dc-title">Calculate GCD and multipliers</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.gmp-gcdext-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>gmp_gcdext</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$a</code></span>
   , <span class="methodparam"><span class="type">resource</span> <code class="parameter">$b</code></span>
   )</div>

  <p class="para rdfs-comment">
   Calculates g, s, and t, such that <em>a*s + b*t = g =
   gcd(a,b)</em>, where gcd is the greatest common divisor. Returns
   an array with respective elements g, s and t.
  </p>
  <p class="para">
   This function can be used to solve linear Diophantine equations in two
   variables. These are equations that allow only integer solutions and have the form:
   <em>a*x + b*y = c</em>.
   For more information, go to the <a href="http://mathworld.wolfram.com/DiophantineEquation.html" class="link external">&raquo;&nbsp;&quot;Diophantine
   Equation&quot; page at MathWorld</a>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.gmp-gcdext-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">a</code></em></dt>

     <dd>

      <p class="para">可以是一个 GMP
数据 <span class="type"><span class="type resouce">resouce</span></span>，或一个可以转换为数值的字符串。</p> 
     </dd>

    
    
     <dt>
<em><code class="parameter">b</code></em></dt>

     <dd>

      <p class="para">可以是一个 GMP
数据 <span class="type"><span class="type resouce">resouce</span></span>，或一个可以转换为数值的字符串。</p> 
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.gmp-gcdext-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   An <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of GMP numbers.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.gmp-gcdext-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3320">
    <p><strong>Example #1 Solving a linear Diophantine equation</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Solve&nbsp;the&nbsp;equation&nbsp;a*s&nbsp;+&nbsp;b*t&nbsp;=&nbsp;g<br />//&nbsp;where&nbsp;a&nbsp;=&nbsp;12,&nbsp;b&nbsp;=&nbsp;21,&nbsp;g&nbsp;=&nbsp;gcd(12,&nbsp;21)&nbsp;=&nbsp;3<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gmp_init</span><span style="color: #007700">(</span><span style="color: #0000BB">12</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gmp_init</span><span style="color: #007700">(</span><span style="color: #0000BB">21</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$g&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gmp_gcd</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$r&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gmp_gcdext</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$check_gcd&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">$g</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">[</span><span style="color: #DD0000">'g'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">$eq_res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gmp_add</span><span style="color: #007700">(</span><span style="color: #0000BB">gmp_mul</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$r</span><span style="color: #007700">[</span><span style="color: #DD0000">'s'</span><span style="color: #007700">]),&nbsp;</span><span style="color: #0000BB">gmp_mul</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$r</span><span style="color: #007700">[</span><span style="color: #DD0000">'t'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">$check_res&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">$g</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">$eq_res</span><span style="color: #007700">));<br /><br />if&nbsp;(</span><span style="color: #0000BB">$check_gcd&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$check_res</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Solution:&nbsp;%d*%d&nbsp;+&nbsp;%d*%d&nbsp;=&nbsp;%d\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$fmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">[</span><span style="color: #DD0000">'s'</span><span style="color: #007700">]),&nbsp;</span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">[</span><span style="color: #DD0000">'t'</span><span style="color: #007700">]),&nbsp;</span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">[</span><span style="color: #DD0000">'g'</span><span style="color: #007700">]));<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Error&nbsp;while&nbsp;solving&nbsp;the&nbsp;equation\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;output:&nbsp;Solution:&nbsp;12*2&nbsp;+&nbsp;21*-1&nbsp;=&nbsp;3<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.gmp-gcd.html">gmp_gcd</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.gmp-hamdist.html">gmp_hamdist</a></div>
 <div class="up"><a href="ref.gmp.html">GMP 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
